<template>
  <div>
    <table
      border="1"
      width="700"
      style="border-collapse: collapse"
    >
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th>
            <input type="checkbox" v-model='isall'/> <span>全选</span>
          </th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr1 
        v-for="(item,index) in goodList" 
        :key="index" :name='item.name' 
        :price='item.price' 
        :num='item.num' 
        :sum='item.price*item.num'
       @add='jia' 
       @reduce="jian"
       @re='del'
       :index='index' 
       :item='item'
       />
       <heji :goodListArr='goodList'/>
      </tbody>
      <tfoot>
        
      </tfoot>
    </table>
  </div>
</template>

<script>
import tr1 from './components/tr'
import heji from './components/heji'
export default {
  components:{tr1,heji},
  data() {
    return {
      goodList: [
        {
          name: "诸葛亮",
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: "蔡文姬",
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: "妲己",
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: "鲁班",
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
      sum:'',
    };
  },
      methods:{
        jia(i){
          // console.log(1);
          this.goodList[i].num++
        },
        jian(i){
          if(this.goodList[i].num<=1) return
            this.goodList[i].num--
          
        },
        del(i){
         this.goodList.splice(i,1)
        }

      },
      computed:{
    isall:{
      get(){
        return this.goodList.every(item=>item.checked)
      },
      set(val){
        this.goodList.forEach(item=>item.checked=val)
      }
    }
  },
};
</script>

<style>
</style>